<?php
class PersonalAction extends PublicAction {
    //首页
	public function index(){
	   if(!session(C('USER_AUTH_KEY')) && !session(C('ADMIN_AUTH_KEY')))
	   $this->redirect('User/login');
	   $uid=getUserInfo('uid');
	   $basic=M('User')->find($uid); 
	   if($basic['status'] == 2){
		   if(session(C("USER_AUTH_KEY")))
			{
				if(session(C("USER_AUTH_KEY"))){
					session(C("USER_AUTH_KEY"),NULL);	
				}
				cookie(C('USER_AUTH_ACCOUNT'),null,-3600);
				cookie(C('USER_AUTH_PWD'),null,-3600);
			}
			$this->redirect('User/login');
	   }
	   $score=$basic['score'];
	   $rule="`lmins`<=$score and `lmaxs`>=$score";
	   $ve=M('Level')->field('lname')->where($rule)->find();
	   $basic['nickname']=msubstr($basic['nickname'], 0, 5);
	   $basic['lname']=$ve['lname'];
	   $model=new Model();
	  $listfr= $model->query("SELECT qx_friend.fuid,qx_friend.fnickname,qx_user.image FROM qx_friend LEFT JOIN qx_user ON qx_friend.fuid = qx_user.uid WHERE qx_friend.uid =$uid limit 0,4"); 
	  $friend=M('Friend')->field('fuid')->where("uid=$uid")->select();
	   
	  foreach($friend as $vo){
	    $oldfriend.=$vo['fuid'].',';
	  }
	   $oldfriend=substr($oldfriend,0,-1);
	   if(strlen($oldfriend)==0) $map="status=1 and uid<>$uid" ;
	   else  $map="status=1 and uid<>$uid and uid not in (".$oldfriend.")";
	   $user=M('User')->where($map)->order('regtime desc')->limit(4)->select();
	  $table=array('图片'=>Picture,'视频'=>Video,'文章'=>Article,'动画'=>Flash,'游戏'=>Game,'挂图'=>Image,'图书'=>Book,'展品'=>Exhibits,'知识题'=>Knowledge);
	  foreach($table as $key=>$door){
	  	$model=D($door);
	  	foreach($friend as $list){
	  	$id=$list['fuid']; 
	  	$rule=array('uid'=>$id,'status'=>1,'is_dele'=>0);
	  	$found=$model->where($rule)->order('uploadtime desc')->first();
	  	 if($found) {
	  	 	$listdoc[$id]['arr'][$door]=$found;
	  	 	$listdoc[$id]['arr'][$door]['china']=$key;
	  	 	$listdoc[$id]['arr'][$door]['type']=$door;
		  	$find=M('User')->where("`uid`=$id")->find();
		  	$listdoc[$id]['nickname']=$find['nickname'];
		  	$listdoc[$id]['image']=$find['image'];
		  	$listdoc[$id]['huid']=$find['uid'];
		  }
	  	}
       $map=array('uid'=>$uid,'is_dele'=>0,'status'=>1);
	   $cates[$door]=$model->where($map)->order('uploadtime desc')->first(); 
	   if($cates[$door])  $cates[$door]['cate']=$door;
	  }
	 // var_dump($listdoc);
	 // exit;
	 $mapur['uid'] = $uid;
   	 $mapur['status'] = 0;
     $urcount = M('Message')->where($mapur)->count();
   	 $this->assign('urcount',$urcount);
	  //dump($listdoc);exit;
	  $this->assign('listfr',$listfr);
	  $this->assign('cates',$cates);
	  $this->assign('user',$user);
	  $this->assign('basic',$basic);
	  $this->assign('listdoc',$listdoc);
	  $this->display();
	}
	
	public function basicinfo(){
	if(!session(C('USER_AUTH_KEY')) && !session(C('ADMIN_AUTH_KEY')))
	$this->redirect('User/login');
	$uid=getUserInfo('uid');
	if($_POST){
	  $info=$_POST;$info['uid']=$uid;
	  D('User')->save($info);
	} 
	  $basic=M('User')->find($uid); 
	//  $basic['nickname']=msubstr($basic['nickname'], 0, 5);
	  $score=$basic['score'];
	   $rule="`lmins`<=$score and `lmaxs`>=$score";
	   $ve=M('Level')->field('lname')->where($rule)->find();
	   $basic['lname']=$ve['lname']; 
	  $this->assign('basic',$basic);
	  $this->display();
	}
	
	public function password(){
	if(!session(C('USER_AUTH_KEY')) && !session(C('ADMIN_AUTH_KEY')))
	$this->redirect('User/login'); 
	$uid=getUserInfo('uid');
	$info=M('User')->find($uid); 
	$info['nickname']=msubstr($info['nickname'], 0, 5);
	$score=$info['score'];
	   $rule="`lmins`<=$score and `lmaxs`>=$score";
	   $ve=M('Level')->field('lname')->where($rule)->find();
	   $info['lname']=$ve['lname']; 
	$uid=getUserInfo('uid');
	if($_POST){
	  $val['password']=md5($_POST['password']); 
	  $val['uid']=$uid; 
	  M('User')->save($val);
	}
	$mapur['uid'] = $uid;
   	$mapur['status'] = 0;
   	$urcount = M('Message')->where($mapur)->count();
   	$this->assign('urcount',$urcount);
	$this->assign('basic',$info);
	$this->display();
	}
	
	public function image(){
	if(!session(C('USER_AUTH_KEY')) && !session(C('ADMIN_AUTH_KEY')))
	$this->redirect('User/login');
	$uid=getUserInfo('uid');
	$info=M('User')->find($uid); 
	$info['nickname']=msubstr($info['nickname'], 0, 5);
	$mapur['uid'] = $uid;
   	$mapur['status'] = 0;
   	$urcount = M('Message')->where($mapur)->count();
   	$this->assign('urcount',$urcount);
	$score=$info['score'];
	   $rule="`lmins`<=$score and `lmaxs`>=$score";
	   $ve=M('Level')->field('lname')->where($rule)->find();
	   $info['lname']=$ve['lname']; 
		if($_GET['suc']){
		   if($_GET['suc']==1) $this->assign('error',$_GET['error']);
		   $this->assign('suc',$_GET['suc']);
		 }else  $this->assign('suc',0);
		  $this->assign('basic',$info);
	     $this->display();
	}
	public function imageupload(){
     if(!empty($_FILES)){
     	$uid=getUserInfo('uid');
     	$info=M('User')->find($uid);
	    $files = $_FILES;
        $imageList = uploadsource('-1','jpg,gif,png,jpeg','./Public/Uploads/PersonImage/',$files,true,'s_','84','84');
        if(is_array($imageList)){
	        import("ORG.Util.Image");
	        $desine = Image::getImageInfo($imageList[0]['savepath'].$imageList[0]['savename']);
	        if($desine['width']>300 || $desine['height']>300){
	        unlink('./Public/Uploads/PersonImage/'.$imageList[0]['savename']);
			unlink('./Public/Uploads/PersonImage/s_'.$imageList[0]['savename']);
			$this->redirect('Personal/image',array('suc'=>3));
	        }
	        $_POST['image']=$imageList[0]['savename'];
        }else{
				$this->error($imageList);
        }
        $model=M('User');
        $data=$_POST; $data['uid']=$uid; 
        $list=$model->save($data);
        $mapur['uid'] = $uid;
	   	$mapur['status'] = 0;
	   	$urcount = M('Message')->where($mapur)->count();
	   	$this->assign('urcount',$urcount);
          if($list){
          			unlink('./Public/Uploads/PersonImage/'.$info['image']); 
					unlink('./Public/Uploads/PersonImage/s_'.$info['image']);
					$this->redirect('Personal/image',array('suc'=>2));
          
          }else{   
		            unlink('./Public/Uploads/PersonImage/'.$info['image']);
					unlink('./Public/Uploads/PersonImage/s_'.$info['image']);
					$this->redirect('Personal/image',array('suc'=>1,'error'=>$model->getError()));
           }
	 }
   }
	public function mysource(){
	   if(!session(C('USER_AUTH_KEY')) && !session(C('ADMIN_AUTH_KEY')))
	   $this->redirect('User/login');
	   $uid=getUserInfo('uid');
	   $info=M('User')->find($uid); 
	   $info['nickname']=msubstr($info['nickname'], 0, 5);
	   $score=$info['score'];
	   $rule="`lmins`<=$score and `lmaxs`>=$score";
	   $ve=M('Level')->field('lname')->where($rule)->find();
	   $info['lname']=$ve['lname'];
	   $table=array('图片'=>Picture,'视频'=>Video,'文章'=>Article,'动画'=>Flash,'游戏'=>Game,'挂图'=>Image,'图书'=>Book,'展品'=>Exhibits,'知识题'=>Knowledge);
	   $idd=array('Picture'=>'pid','Game'=>'gid','Video'=>'vid','Article'=>'aid','Flash'=>'flaid','Image'=>'iid','Book'=>'bid','Exhibits'=>'eid','Knowledge'=>'kid');
	   foreach($table as $key=>$door){
	  	$model=D($door); $id=$idd[$door];
	  	$map=array('uid'=>$uid,'is_dele'=>0,'status'=>1);
	  	if(in_array($door,array(Picture,Image,Exhibits))){
	        $found=$model->field("`".$id."`,`title`,`url`,`point`")->where($map)->order('uploadtime desc')->limit(4)->select(); //dump($found);
	        foreach($found as $ke=>$f){
		  	  $arr=explode(',',$f['url']); 
		  	  $found[$ke]['id']="$id";
		  	  $found[$ke]['imageurl']=$arr[0];
		  	  
		  	}
	  	}else 
	   $found=$model->field("`".$id."`,`title`,`imageurl`,`point`")->where($map)->order('uploadtime desc')->limit(4)->select();
	   if($found){
	   		foreach ($found as $k1=>$v1)
	   		{
	   			$found[$k1]['vid']=$v1[$id];
	   			$found[$k1]['id']="$id";
	   		}
		   	$cates[$door]['arr']=$found;  
		   	$cates[$door]['cate']=$door;
		   	$cates[$door]['ch']=$key;
		   	
	   }
	}   
	  $model=new Model();
	  $listfr= $model->query("SELECT qx_friend.fuid,qx_friend.fnickname,qx_user.image FROM qx_friend LEFT JOIN qx_user ON qx_friend.fuid = qx_user.uid WHERE qx_friend.uid =$uid LIMIT 0 , 5"); 
	  $friend=M('Friend')->field('fuid')->where("uid=$uid")->select();
	    foreach($friend as $vo){
	    $oldfriend.=$vo['fuid'].',';
	  }
	   $oldfriend=substr($oldfriend,0,-1);
	   if(strlen($oldfriend)==0) $map="status=1 and uid<>$uid" ;
	   else  $map="status=1 and uid<>$uid and uid not in (".$oldfriend.")";
	   $user=M('User')->where($map)->order('regtime desc')->limit(4)->select();
	   $mapur['uid'] = $uid;
	   	$mapur['status'] = 0;
	   	$urcount = M('Message')->where($mapur)->count();
	   	$this->assign('urcount',$urcount);
	    $this->assign('basic',$info);
	    $this->assign('cates',$cates);//dump($cates);exit;
	    $this->assign('user',$user);
	    $this->assign('listfr',$listfr);
	    $this->display();
	}
	
	public function favorite(){
	  if(!session(C('USER_AUTH_KEY')) && !session(C('ADMIN_AUTH_KEY')))
	   $this->redirect('User/login');
	   $uid=getUserInfo('uid');
	   $info=M('User')->find($uid); 
	   $info['nickname']=msubstr($info['nickname'], 0, 5);
	   $score=$info['score'];
	   $rule="`lmins`<=$score and `lmaxs`>=$score";
	   $ve=M('Level')->field('lname')->where($rule)->find();
	   $info['lname']=$ve['lname'];
	   $table=array('图片'=>Picture,'视频'=>Video,'文章'=>Article,'动画'=>Flash,'游戏'=>Game,'挂图'=>Image,'图书'=>Book,'展品'=>Exhibits,'知识题'=>Knowledge);
       $num=array('1'=>'图片','2'=>'视频','3'=>'动画','4'=>'游戏','5'=>'文章','6'=>'挂图','7'=>'图书','8'=>'展品','9'=>'知识题');
	   $idd=array('Picture'=>'pid','Game'=>'gid','Video'=>'vid','Article'=>'aid','Flash'=>'flaid','Image'=>'iid','Book'=>'bid','Exhibits'=>'eid','Knowledge'=>'kid');
       $cates=M('Favorite')->where("`uid`=$uid")->select(); //dump($cates);exit;
	   foreach($cates as $v){
	     $cat=$table[$num[$v['fatype']]];  
	     $id=$idd[$cat];
	     if(in_array($cat,array(Picture,Image,Exhibits))){
	     $found=M($cat)->field("`url`,`point`")->where($id."=".$v['favid'])->find();
	     $found['imageurl']=$found['url'];
	     } 
	     else
	     $found=M($cat)->field("`imageurl`,`point`")->where($id."=".$v['favid'])->find();
	     foreach($found as $f){
	       $v['point']=$found['point'];
	       $arry=explode(',',$found['imageurl']);
	       $v['imageurl']=$arry[0];
	     }
		 $v['flvid'] = $v['favid'];
	     $fav[$cat]['arr'][]=$v;
	     $fav[$cat]['ch']=$num[$v['fatype']];
	     $fav[$cat]['cate']=$cat;
	   }
      $model=new Model();
	  $listfr= $model->query("SELECT qx_friend.fuid,qx_friend.fnickname,qx_user.image FROM qx_friend LEFT JOIN qx_user ON qx_friend.fuid = qx_user.uid WHERE qx_friend.uid =$uid LIMIT 0 , 5"); 
	  $friend=M('Friend')->field('fuid')->where("uid=$uid")->select();
	    foreach($friend as $vo){
	    $oldfriend.=$vo['fuid'].',';
	  }
	   $oldfriend=substr($oldfriend,0,-1);
	   if(strlen($oldfriend)==0) $map="status=1 and uid<>$uid" ;
	   else  $map="status=1 and uid<>$uid and uid not in (".$oldfriend.")";
	   $user=M('User')->where($map)->order('regtime desc')->limit(4)->select();
	   $mapur['uid'] = $uid;
	   	$mapur['status'] = 0;
	   	$urcount = M('Message')->where($mapur)->count();
	   	$this->assign('urcount',$urcount);
	    $this->assign('basic',$info);
	    $this->assign('cates',$fav); //dump($fav);exit;
	    $this->assign('user',$user);
	    $this->assign('listfr',$listfr);
	    $this->display();
	}
    
	public function friend(){
	   if(!session(C('USER_AUTH_KEY')) && !session(C('ADMIN_AUTH_KEY')))
	   $this->redirect('User/login');
	   $uid=getUserInfo('uid');
	   $info=M('User')->find($uid); $score=$info['score'];
	   $info['nickname']=msubstr($info['nickname'], 0, 5);
	   $rule="`lmins`<=$score and `lmaxs`>=$score";
	   $ve=M('Level')->field('lname')->where($rule)->find();
	   $info['lname']=$ve['lname'];
	   
       import("ORG.Util.Page");// 导入分页类
      // 进行分页数据查询 当前的页数使用 $_GET[p]获取
      $page= isset($_GET['p'])?$_GET['p']:1; $pages=($page-1)*21;
      $model=new Model();
       if($_POST['keyword'])
       $friend= $model->query("SELECT qx_friend.fuid,qx_friend.fnickname,qx_user.image FROM qx_friend LEFT JOIN qx_user ON qx_friend.fuid = qx_user.uid WHERE qx_friend.uid =$uid and qx_friend.fnickname like '%".$_POST['keyword']."%' LIMIT $pages , 21"); 
       else
	   $friend= $model->query("SELECT qx_friend.fuid,qx_friend.fnickname,qx_user.image FROM qx_friend LEFT JOIN qx_user ON qx_friend.fuid = qx_user.uid WHERE qx_friend.uid =$uid LIMIT $pages , 21"); 
       $count= M('Friend')->where("`uid`=$uid")->count();
	   $Page= new Page($count,21);// 实例化分页类 传入总记录数和每页显示的记录数
	   $show = $Page->show();// 分页显示输出
	   //dump($friend);exit;
	   $mapur['uid'] = $uid;
	   	$mapur['status'] = 0;
	   	$urcount = M('Message')->where($mapur)->count();
	   	$this->assign('urcount',$urcount);
	   $this->assign('friend',$friend);
	   $this->assign('show',$show);
	   $this->assign('basic',$info);
	   $this->display();
	}
     
	 public function hiszoon(){
	  if(!session(C('USER_AUTH_KEY')) && !session(C('ADMIN_AUTH_KEY')))
	   $this->redirect('User/login');
	   if(isset($_GET['id'])){
	   $uid=$_GET['id'];
	   $loguid=getUserInfo('uid');
	   if($uid==$loguid) $this->redirect('Personal/index');
	   if(!isfriend($uid,$loguid))  $this->assign('adf',1);
	   $basic=M('User')->find($uid); $score=$basic['score'];
	   $rule="`lmins`<=$score and `lmaxs`>=$score";
	   $ve=M('Level')->field('lname')->where($rule)->find();
	   $basic['nickname']=msubstr($basic['nickname'], 0, 5);
	   $basic['lname']=$ve['lname']; 
	   $model=new Model();
	  $listfr= $model->query("SELECT *FROM qx_friend LEFT JOIN qx_user ON qx_friend.fuid = qx_user.uid WHERE qx_friend.uid =$uid LIMIT 0 , 5");
		$squid=getUserInfo('uid');
	  $friend=M('Friend')->field('fuid')->where("`uid`=$squid")->select();
	  foreach($friend as $vo){
	    $oldfriend.=$vo['fuid'].',';
	  }
	   $oldfriend=substr($oldfriend,0,-1);
	   if(strlen($oldfriend)==0) $map="status=1 and uid<>$squid" ;
	   else  $map="status=1 and uid<>$squid and uid not in (".$oldfriend.")";
	   $user=M('User')->where($map)->order('regtime desc')->limit(4)->select();
	  $table=array('图片'=>Picture,'视频'=>Video,'文章'=>Article,'动画'=>Flash,'游戏'=>Game,'挂图'=>Image,'图书'=>Book,'展品'=>Exhibits,'知识题'=>Knowledge);
	  foreach($table as $key=>$door){
	  	$model=D($door);
	  	foreach($listfr as $list){
	  	$id=$list['fuid']; 
	  	$rule=array('uid'=>$id,'status'=>1,'is_dele'=>0);
	  	$found=$model->where($rule)->order('uploadtime desc')->first();
	  	 if($found) {
	  	 	$listdoc[$id]['arr'][$door]=$found;
	  	 	$listdoc[$id]['arr'][$door]['china']=$key;
		  	$find=M('User')->where("`uid`=$id")->find();
		  	$listdoc[$id]['nickname']=$find['nickname'];
		  	$listdoc[$id]['image']=$find['image'];
		  }
	  	}
       $map=array('uid'=>$uid,'is_dele'=>0,'status'=>1);
	   $cates[$door]=$model->where($map)->order('uploadtime desc')->first(); 
	   if($cates[$door])  $cates[$door]['cate']=$door;
	  }
	 
	  $this->assign('listfr',$listfr);
	  $this->assign('cates',$cates);
	  $this->assign('user',$user);
	  $this->assign('basic',$basic);
	  $this->assign('listdoc',$listdoc);
	  $this->display();
	  }else $this->redirect('Personal/index');
	 }
	 
     public function hisfavorite(){
	  if(!session(C('USER_AUTH_KEY')) && !session(C('ADMIN_AUTH_KEY')))
	   $this->redirect('User/login');
	   if(isset($_GET['id'])){
	   $uid=$_GET['id'];
	   $info=M('User')->find($uid); 
	   $info['nickname']=msubstr($info['nickname'], 0, 5);
	   $score=$info['score'];
	   $rule="`lmins`<=$score and `lmaxs`>=$score";
	   $ve=M('Level')->field('lname')->where($rule)->find();
	   $info['lname']=$ve['lname'];
	   $table=array('图片'=>Picture,'视频'=>Video,'文章'=>Article,'动画'=>Flash,'游戏'=>Game,'挂图'=>Image,'图书'=>Book,'展品'=>Exhibits,'知识题'=>Knowledge);
       $num=array('1'=>'图片','2'=>'视频','3'=>'动画','4'=>'游戏','5'=>'文章','6'=>'挂图','7'=>'图书','8'=>'展品','9'=>'知识题');
	   $idd=array('Picture'=>'pid','Game'=>'gid','Video'=>'vid','Article'=>'aid','Flash'=>'flaid','Image'=>'iid','Book'=>'bid','Exhibits'=>'eid','Knowledge'=>'kid');
       $cates=M('Favorite')->where("`uid`=$uid")->select(); //dump($cates);exit;
	   foreach($cates as $v){
	     $cat=$table[$num[$v['fatype']]];  
	     $id=$idd[$cat];
	     if(in_array($cat,array(Picture,Image,Exhibits))){
	     $found=M($cat)->field("`url`,`point`")->where($id."=".$v['favid'])->find();
	     $found['imageurl']=$found['url'];
	     }else
	     $found=M($cat)->field("`imageurl`,`point`")->where($id."=".$v['favid'])->find();
	     foreach($found as $f){
	       $v['point']=$found['point'];
	       $arry=explode(',',$found['imageurl']);
	       $v['imageurl']=$arry[0];
	     }
	     $fav[$cat]['arr'][]=$v;
	     $fav[$cat]['ch']=$num[$v['fatype']];
	     $fav[$cat]['cate']=$cat;
	   }

	   $model=new Model();
	  $listfr= $model->query("SELECT qx_friend.fuid,qx_friend.fnickname,qx_user.image FROM qx_friend LEFT JOIN qx_user ON qx_friend.fuid = qx_user.uid WHERE qx_friend.uid =$uid LIMIT 0 , 5"); 
	  $dluid = getUserInfo('uid');
	  $friend=M('Friend')->field('fuid')->where("uid=$dluid")->select();
	    foreach($friend as $vo){
	    $oldfriend.=$vo['fuid'].',';
	  }
	   $oldfriend=substr($oldfriend,0,-1);
	   if(strlen($oldfriend)==0) $map="status=1 and uid<>$uid" ;
	   else  $map="status=1 and uid<>$dluid and uid not in (".$oldfriend.")";
	   $user=M('User')->where($map)->order('regtime desc')->limit(4)->select();
	    $this->assign('basic',$info);
	    $this->assign('cates',$fav); //dump($fav);exit;
	    $this->assign('user',$user);
	    $this->assign('listfr',$listfr);
	    $this->display();
	   }else $this->redirect('Personal/index');
     }
     
      public function hisfriend(){
	   if(!session(C('USER_AUTH_KEY')) && !session(C('ADMIN_AUTH_KEY')))
	   $this->redirect('User/login');
	   if(isset($_GET['id'])){
	   $uid=$_GET['id'];
	   $loguid=getUserInfo('uid');
	   if(!isfriend($uid,$loguid))  $this->assign('adf',1);
	   $info=M('User')->find($uid); $score=$info['score'];
	   $info['nickname']=msubstr($info['nickname'], 0, 5);
	   $rule="`lmins`<=$score and `lmaxs`>=$score";
	   $ve=M('Level')->field('lname')->where($rule)->find();
	   $info['lname']=$ve['lname'];
	   
       import("ORG.Util.Page");// 导入分页类
      // 进行分页数据查询 当前的页数使用 $_GET[p]获取
       $page= isset($_GET['p'])?$_GET['p']:1;  $pages=($page-1)*21;
       $count= M('Friend')->where("`uid`=$uid")->count();
	   $Page= new Page($count,21);// 实例化分页类 传入总记录数和每页显示的记录数
       $model=new Model();
       $friend= $model->query("SELECT qx_friend.fuid,qx_friend.fnickname,qx_user.image FROM qx_friend LEFT JOIN qx_user ON qx_friend.fuid = qx_user.uid WHERE qx_friend.uid =$uid LIMIT $pages , 21");
	   $show = $Page->show();// 分页显示输出
	   //dump($friend);exit;
	   $this->assign('friend',$friend);
	   $this->assign('show',$show);
	   $this->assign('basic',$info);
	   $this->display();
	  }else $this->redirect('Personal/index');
    }
	public function message(){
	   if(!session(C('USER_AUTH_KEY')) && !session(C('ADMIN_AUTH_KEY')))
	   $this->redirect('User/login');
	   $uid=getUserInfo('uid');
	   $info=M('User')->find($uid); $score=$info['score'];
	   $info['nickname']=msubstr($info['nickname'], 0, 5);
	   $rule="`lmins`<=$score and `lmaxs`>=$score";
	   $ve=M('Level')->field('lname')->where($rule)->find();
	   $info['lname']=$ve['lname'];
	    import("ORG.Util.Page");// 导入分页类
	   // 进行分页数据查询 当前的页数使用 $_GET[p]获取
     // $page=$_GET['p']= isset($_GET['p'])?$_GET['p']:1; $pages=($page-1)*21;
       $count= M('Message')->where("`uid`=$uid")->count();
	   $Page= new Page($count,21);// 实例化分页类 传入总记录数和每页显示的记录数
       $msg=M('Message')->where("`uid`=$uid")->limit($Page->firstRow.','.$Page->listRows)->select();
       	foreach($msg as $key => $v){
       		$puname = M('User')->field('nickname')->find($v['puid']);
       		$msg[$key]['puname'] = $puname['nickname'];
      	}
	   $show = $Page->show();// 分页显示输出
	   $mapur['uid'] = $uid;
	   $mapur['status'] = 0;
	   $urcount = M('Message')->where($mapur)->count();
	   $this->assign('urcount',$urcount);
	   //dump($friend);exit;
	   $this->assign('msg',$msg);
	   $this->assign('show',$show);
	   $this->assign('basic',$info);
	   $this->display();
	}
	
	public function delemessage(){
		if(!session(C('USER_AUTH_KEY')) && !session(C('ADMIN_AUTH_KEY')))
	    $this->redirect('User/login');
	    $mid = $_GET['mid'];
	    if($mid){
	    	$map['mid'] = $mid;
	    	M('Message')->where($map)->delete();
	    }
	    $this->redirect('Personal/message'); 
	}
	
	public function messageinfo(){
		if(!session(C('USER_AUTH_KEY')) && !session(C('ADMIN_AUTH_KEY')))
	    $this->redirect('User/login');
	    $mid = $_GET['mid'];
	    if($mid){
		    $uid=getUserInfo('uid');
		    $info=M('User')->find($uid); $score=$info['score'];
		    $info['nickname']=msubstr($info['nickname'], 0, 5);
		    $rule="`lmins`<=$score and `lmaxs`>=$score";
		    $ve=M('Level')->field('lname')->where($rule)->find();
		    $info['lname']=$ve['lname'];
		    $map['mid'] = $mid;
		    $data['status'] = 1;
		    M('Message')->where($map)->save($data);
		    $msginfo = M('Message')->where($map)->find();
		    $puname = M('User')->field('nickname')->find($msginfo['puid']);
       		$msginfo['puname'] = $puname['nickname'];
		    $mapur['uid'] = $uid;
		    $mapur['status'] = 0;
		    $urcount = M('Message')->where($mapur)->count();
		    $this->assign('msginfo',$msginfo);
		    $this->assign('urcount',$urcount);
		    $this->assign('basic',$info);
		    $this->display();
	    }else{
	    	$this->redirect('Personal/message');
	    }
	}
	
	public function somemore(){
	 if(!session(C('USER_AUTH_KEY')) && !session(C('ADMIN_AUTH_KEY')))
	   $this->redirect('User/login');
	   if(isset($_GET['cate'])){
	   $uid=getUserInfo('uid');
	   $info=M('User')->find($uid); 
	   $info['nickname']=msubstr($info['nickname'], 0, 5);
	   $score=$info['score'];
	   $rule="`lmins`<=$score and `lmaxs`>=$score";
	   $ve=M('Level')->field('lname')->where($rule)->find();
	   $info['lname']=$ve['lname'];
	   
	   $model=new Model();
	  $listfr= $model->query("SELECT qx_friend.fuid,qx_friend.fnickname,qx_user.image FROM qx_friend LEFT JOIN qx_user ON qx_friend.fuid = qx_user.uid WHERE qx_friend.uid =$uid LIMIT 0 , 5"); 
	  $friend=M('Friend')->field('fuid')->where("uid=$uid")->select();
	    foreach($friend as $vo){
	    $oldfriend.=$vo['fuid'].',';
	  }
	   $oldfriend=substr($oldfriend,0,-1);
	   if(strlen($oldfriend)==0) $map="status=1 and uid<>$uid" ;
	   else  $map="status=1 and uid<>$uid and uid not in (".$oldfriend.")";
	   $user=M('User')->where($map)->order('regtime desc')->limit(4)->select();
	     
	     $door=$_GET['cate'];
	     import("ORG.Util.Page");
	   //  $page= isset($_GET['p'])?$_GET['p']:1; //$pages=($page-1)*20;
	     $rule=array('uid'=>$uid,'is_dele'=>0,'status'=>1);
	    $count=M($door)->where($rule)->count();
	    $Page= new Page($count,20);
	    $upload['arr']= M($door)->where($rule)->order('uploadtime desc')->limit($Page->firstRow.','.$Page->listRows)->select();
	    $upload['cate']=$door;
	    $show=$Page->show();
	    $idd=array('Picture'=>'pid','Game'=>'gid','Video'=>'vid','Article'=>'aid','Flash'=>'flaid','Image'=>'iid','Book'=>'bid','Exhibits'=>'eid','Knowledge'=>'kid');
	    $table=array('图片'=>Picture,'视频'=>Video,'文章'=>Article,'动画'=>Flash,'游戏'=>Game,'挂图'=>Image,'图书'=>Book,'展品'=>Exhibits,'知识题'=>Knowledge);
	    foreach($table as $key=>$v){
	     if($v==$door){
	     	$upload['ch']=$key;
			$upload['en']=strtolower($v);
	     }
	    }
	   if(in_array($upload['cate'],array(Picture,Image,Exhibits))){
	        foreach($upload['arr'] as $ke=>$f){
		  	  $arr=explode(',',$f['url']); 
		  	  $upload['arr'][$ke]['imageurl']=$arr[0];
	        }  	  
		}
		foreach($upload['arr'] as $keys => $vals){
			$upload['arr'][$keys]['vid'] = $vals[$idd[$door]];
		}
		$mapur['uid'] = $uid;
	   	$mapur['status'] = 0;
	   	$urcount = M('Message')->where($mapur)->count();
	   	$this->assign('urcount',$urcount);
	    $this->assign('basic',$info);
	    $this->assign('user',$user);
	    $this->assign('listfr',$listfr);
	    $this->assign('upload',$upload);
	    $this->assign('show',$show);
	    $this->display();
	   }
	}
	
	public function morestore(){
	  if(!session(C('USER_AUTH_KEY')) && !session(C('ADMIN_AUTH_KEY')))
	   $this->redirect('User/login');
	   if(isset($_GET['cate'])){
	   	$cate=$_GET['cate'];
	   $uid=isset($_GET['id'])?$_GET['id']:getUserInfo('uid');
	   $info=M('User')->find($uid); 
	   $info['nickname']=msubstr($info['nickname'], 0, 5);
	   $score=$info['score'];
	   $rule="`lmins`<=$score and `lmaxs`>=$score";
	   $ve=M('Level')->field('lname')->where($rule)->find();
	   $info['lname']=$ve['lname'];
	   $table=array('Picture'=>'图片','Video'=>'视频','Article'=>'文章','Flash'=>'动画','Game'=>'游戏','Image'=>'挂图','Book'=>'图书','Exhibits'=>'展品','Knowledge'=>'知识题');
       $num=array('Picture'=>'1','Video'=>'2','Flash'=>'3','Game'=>'4','Article'=>'5','Image'=>'6','Book'=>'7','Exhibits'=>'8','Knowledge'=>'9');
	   $idd=array('Picture'=>'pid','Game'=>'gid','Video'=>'vid','Article'=>'aid','Flash'=>'flaid','Image'=>'iid','Book'=>'bid','Exhibits'=>'eid','Knowledge'=>'kid');
       import("ORG.Util.Page");// 导入分页类
       $count=M('Favorite')->where("`uid`=$uid and `fatype`=$num[$cate]")->count();
       $Page=new Page($count,20);
       $show=$Page->show();
       $cates=M('Favorite')->where("`uid`=$uid and `fatype`=$num[$cate]")->limit($Page->firstRow.','.$Page->listRows)->select();
       foreach($cates as $vo){
       	 $id=$idd[$cate];
       	 if(in_array($cate,array(Picture,Image,Exhibits))){
       	 $found= M($cate)->field("`point`,`url`")->where($id.'='.$vo['favid'])->find();
       	 $arr=explode(',',$found['url']);
       	 $vo['imageurl']=$arr[0]; $vo['point']=$found['point'];
       	 $fav[]=$vo;
       	 }
       	 else{
       	  $found= M($cate)->field("`point`,`imageurl`")->where($id.'='.$vo['favid'])->find();
       	  $vo['imageurl']=$found['imageurl']; $vo['point']=$found['point'];
       	  $fav[]=$vo;
       	 }
        }
        $disp['ch']=$table[$cate];
        $disp['cate']=$cate;
       $model=new Model();
	  $listfr= $model->query("SELECT qx_friend.fuid,qx_friend.fnickname,qx_user.image FROM qx_friend LEFT JOIN qx_user ON qx_friend.fuid = qx_user.uid WHERE qx_friend.uid =$uid LIMIT 0 , 5"); 
	  $friend=M('Friend')->field('fuid')->where("uid=$uid")->select();
	    foreach($friend as $vo){
	    $oldfriend.=$vo['fuid'].',';
	  }
	   $oldfriend=substr($oldfriend,0,-1);
	   if(strlen($oldfriend)==0) $map="status=1 and uid<>$uid" ;
	   else  $map="status=1 and uid<>$uid and uid not in (".$oldfriend.")";
	   $user=M('User')->where($map)->order('regtime desc')->limit(4)->select();
	   $mapur['uid'] = $uid;
	   	$mapur['status'] = 0;
	   	$urcount = M('Message')->where($mapur)->count();
	   	if($_GET['tys']){
	   		$this->assign('tys',$_GET['tys']);
	   	}
	   	$this->assign('uid',$uid);
	   	$this->assign('urcount',$urcount);
	    $this->assign('basic',$info);
	    $this->assign('cates',$fav);
	     $this->assign('show',$show);// 赋值分页输出
        $this->assign('disp',$disp);
	    $this->assign('user',$user);
	    $this->assign('listfr',$listfr);
	    $this->display();
	   }else $this->redirect('Personal/index');
	}
	public function checkpass(){
	  $val=md5($_POST['val']);
	  $uid=getUserInfo('uid');
	  $pass=M('User')->field('password')->find($uid);
	  if($val==$pass['password']) echo 1;
	  else echo 0;
	}
	
	public function addfr(){
	 $addfr=array();
	 $addfr['fuid']=$_POST['fuid'];
	 $msg=M('User')->find($_POST['fuid']);
	 $addfr['fnickname']=$msg['nickname'];
	 //$addfr['fnickname']=$_POST['fnickname'];
	 $addfr['uid']=getUserInfo('uid');
	 $model=D('Friend');
	 if($model->where($addfr)->find()){
	 	echo 2;
	 }else{
	      if($model->create($addfr)){
		  $fr=$model->add();
		  if($fr) echo 1;
		  else echo 0;
		 }else echo 0;
	 }
	 
	}
	public function delefr(){
	 $model=M('Friend');
	 $data['fuid']=$_POST['fuid'];
	 $data['uid']=getUserInfo('uid');
	  if($model->create($data)){
	  $dele=$model->where($data)->delete();
	  if($dele) echo 1;
	  else echo 0;
	  }else echo 0;
	}
	
	public function send(){
	 $val['puid']=getUserInfo('uid');
	 $val['uid']=$_POST['fuid'];
	 $val['title']=$_POST['title'];
	 $val['content']=$_POST['content'];
	 if(!$val['title'] || !$val['content']){
	 	echo 2;
	 	exit;
	 }
	 $model=D('Message');
	if( $model->create($val)){
	  if($model->add()) echo 1;
	  else echo 0;
	 }else echo 0;
	}
	
	/**
	 * 个人中心搜索用户
	 * @author 沈威
	 * @tel 15005164276
	 * @email 403382132@qq.com
	 * Enter description here ...
	 */
	public function search()
	{
		if(!session(C('USER_AUTH_KEY')) && !session(C('ADMIN_AUTH_KEY')))
	   $this->redirect('User/login');
	   $uid=getUserInfo('uid');
	   $info=M('User')->find($uid); 
	   $info['nickname']=msubstr($info['nickname'], 0, 5);
	   $score=$info['score'];
	   $rule="`lmins`<=$score and `lmaxs`>=$score";
	   $ve=M('Level')->field('lname')->where($rule)->find();
	   $info['lname']=$ve['lname'];
	   
       import("ORG.Util.Page");// 导入分页类
      // 进行分页数据查询 当前的页数使用 $_GET[p]获取
      $page= isset($_GET['p'])?$_GET['p']:1; $pages=($page-1)*21;
      $model=M('User');
       if($_GET['keyword'])
       {
       		if($_GET['keyword'] == '%')
       		{
       			$map['nickname']=array('like','%\\'.$_GET['keyword'].'%');
       		}else{
       			$map['nickname']=array('like','%'.$_GET['keyword'].'%');
       		}
       		
       }
       $map['status'] = 1;
	   $map['uid'] = array('neq',$uid);
       $count= M('User')->where($map)->count();
	   $page= new Page($count,21);// 实例化分页类 传入总记录数和每页显示的记录数
	   $list=$model->where($map)->limit($page->firstRow.",".$page->listRows)->order("regtime desc")->select();
	   //判定是否是已经添加的好友
	   $model_friend=M('Friend');
	   foreach ($list as $k => $v)
	   {
	   		$cap['uid']=$uid;
	   		$cap['fuid']=$v['uid'];
	   		$isfriend=$model_friend->where($cap)->find();
	   		if($isfriend)
	   		{
	   			$list[$k]['isfriend']=1;
	   		}
	   		else
	   		{
	   			$list[$k]['isfriend']=0;
	   		}
	   }
	   
	   $show = $page->show();// 分页显示输出
	   $mapur['uid'] = $uid;
	   	$mapur['status'] = 0;
	   	$urcount = M('Message')->where($mapur)->count();
	   	$this->assign('urcount',$urcount);
	   //dump($friend);exit;
	   if($list){
	   		$flsta = 1;
	   }else{
	   		$flsta = 0;
	   }
	   $this->assign('flsta',$flsta);
	   $this->assign('friend',$list);
	   $this->assign('show',$show);
	   $this->assign('basic',$info);
	   $this->display();
	}
}